import { apiUrl } from './const.ts';
import qs from 'qs';

import * as auth from '../auth-provider';

interface Config extends RequestInit {
    token?: string;
    data?: object;
}

export const http = async (
    endpoint: string,
    { data, token, headers, ...customConfig }: RequestInit
) => {
    const config = {
        method: 'GET',
        headers: {
            Authorization: token ? `Bearer ${token}` : '',
            'Content-Type': data ? 'application/json' : '',
        },
        ...customConfig,
    };
    if (config.method.toUpperCase() === 'GET') {
        endpoint += `${qs.stringify(data)}`;
    } else {
        config.body = JSON.stringify(data);
    }
    return window.fetch(`${apiUrl}/${endpoint}`, config).then(async (res) => {
        if (res.status === 401) {
            await auth.logout();
            window.location.reload();
            return Promise.reject({ message: '请重新登录' });
        }
        const data = await res.json();
        if (res.ok) {
            return data;
        } else {
            return Promise.reject(data);
        }
    });
};
